<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

if (version_compare(phpversion(), '5.2.0', '<') === true)
{
    echo '<h1>You have an invalid PHP version.</h1>';
    echo '<p>Codizer supports PHP 5.2.0 or newer. Please upgrade your PHP version</p>';
    echo '<hr />';
    echo 'Current PHP Version : '.phpversion();
    exit;
}

// Set the umask to define rights on paths
umask(002);

// Constants definitions
define('DS', DIRECTORY_SEPARATOR);
define('BASE_DIR', realpath(dirname(__FILE__).DS.'..'));
define('FRAMEWORK_HOME', BASE_DIR.DS.'codizer');
define('MODULES_DIR', BASE_DIR.DS.'modules');
define('WEBAPP_DIR', BASE_DIR.DS.'webapp');
define('CACHE_DIR', BASE_DIR.DS.'webapp'.DS.'cache');
define('WEBAPP_MODULES_DIR', WEBAPP_DIR.DS.'modules');
define('CONFIG_DIR', WEBAPP_DIR.DS.'config');
define('LOGS_DIR', WEBAPP_DIR.DS.'logs');
define('HTTP_DIR', BASE_DIR.DS.'www');
define('CLI_MODE', false);
if(isset($_SERVER['HTTP_HOST']))
    define('HTTP_HOST', $_SERVER['HTTP_HOST']);

// Loading Exception class and Autoload class
require_once FRAMEWORK_HOME.DS.'exception'.DS.'ErrorHandler.class.php';
require_once FRAMEWORK_HOME.DS.'exception'.DS.'BaseException.class.php';

// Sets error handlers
@set_error_handler(array(new f_exception_ErrorHandler(),'errorHandler'));
@set_exception_handler(array(new f_exception_BaseException(),'exceptionHandler'));

// Loading the __autoload PHP5 magic method
require_once(FRAMEWORK_HOME.DS.'Autoload.class.php');

// Framework is not well installed...
if(!is_dir(WEBAPP_DIR.DS.'config'))
{
    f_builder_ApplicationBuilder::getInstance()->initApplication();

    // if there is an installation action...
    if(f_core_Controller::isFileFromClass('m_install_actions_InstallAction'))
    {
        $config = f_loader_Config::getInstance();

        $config->setParameter('default', 'default_module', 'install');
        $config->setParameter('default', 'default_action', 'Install');
        $config->setParameter('default', 'defaultDocumentLang', '');

        $config->save();

        date_default_timezone_set($config->getParameter('default', 'timeZone'));

        f_builder_RoutingBuilder::getInstance()->buildRouting();
        f_builder_TargetParser::getInstance()->buildTargets();

        f_core_Controller::getInstance()->setModule('install')->setAction('Install')->dispatch();
       exit;
    }

}

// Loading the profile parameters
$config = f_loader_Config::getInstance()->getParameters('default');

// Get http or https for absolute urls
$http = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
if(isset($config['httpRootFolder']))
{
    // SET BASE URLS (absolute or relative) WITHOUT THE LAST SLASH
    // URL_ABSOLUTE = http://www.myhost.com/mysubfolder
    // URL_RELATIVE = /mysubfolder

    // Remove the slash at the end of subfolder
	if(substr($config['httpRootFolder'], -1) == '/')
    {
        $config['httpRootFolder'] = substr($config['httpRootFolder'], 0, strlen($config['httpRootFolder'])-1);
    }

    // Put a slash if necessary
	if(strlen($config['httpRootFolder']) > 1 && substr($config['httpRootFolder'], 0, 1) != '/')
    {
        $config['httpRootFolder'] = '/'.$config['httpRootFolder'];
    }

    // Set absolute URL without http
    define('URL_ABSOLUTE_WO_HTTP', $_SERVER['HTTP_HOST'].$config['httpRootFolder']);
    // Set absolute URL    
	define('URL_ABSOLUTE', $http.$_SERVER['HTTP_HOST'].$config['httpRootFolder']);
    // Set relative URL
    define('URL_RELATIVE', $config['httpRootFolder']);

}
else
{
    // Set absolute URL without http
    define('URL_ABSOLUTE_WO_HTTP', $_SERVER['HTTP_HOST'].$config['httpRootFolder']);
    // Set absolute URL
    define('URL_ABSOLUTE', $http.$_SERVER['HTTP_HOST']);
    // Set relative URL
    define('URL_RELATIVE', '');
}

if(isset($config['frontTheme']))
{
    define('FRONT_THEME', $config['frontTheme']);
}

if(isset($config['backTheme']))
{
    define('BACK_THEME', $config['backTheme']);
}

// Set the timezone defined in config file
date_default_timezone_set($config['timeZone']);

// Init the session
f_core_Session::getInstance();